﻿using KPI.Api.Apis;
using KPI.Data;
using KPI.Entity;
using KPI.Entity.Entitys.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using TYBase;
using WPF_TY_UI;

namespace CssdAssistantClient.Views
{
    public partial class ViewEditQuestion : TYWindow
    {
        ViewCreateQuestionModel vm = new ViewCreateQuestionModel();
        public ViewEditQuestion(string qtId)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.Loaded += ViewCreateQuestion_Loaded;
            this.qbId = qtId;
        }

        private async void ViewCreateQuestion_Loaded(object sender, RoutedEventArgs e)
        {
            var rData = await Run(() =>
            {
                return QuestionBankApi.GetQuestionAllInfo(this.qbId);
            });

            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            if (rData.data == null)
            {
                MessageBox.Show("数据为空!");
                return;
            }
            vm.FromData(rData.data);

        }

        private List<string> listSingle = new List<string>();
        private List<string> listMutil = new List<string>();
        //private List<QuestionItemInfoVO> listOption;
        private readonly string qbId;


        private void LSBComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            vm.ListOption.Clear();
            switch (vm.SelectedQtType.Key)
            {
                case (int)(EnumQtType.SingleSelect):
                case (int)(EnumQtType.MutilSelect):
                    if (vm.Data.qtType == (int)(EnumQtType.SingleSelect) || vm.Data.qtType == (int)(EnumQtType.MutilSelect))
                    {
                        vm.Data.listItemInfo.ForEach(x =>
                        {
                            vm.ListOption.Add(new OptionModel { OptionName = x.content, Score = x.score });
                        });
                        if (vm.ListOption.Count == 0)
                        {
                            vm.ListOption.Add(new OptionModel { OptionName = "" });
                            vm.ListOption.Add(new OptionModel { OptionName = "" });
                        }
                    }
                    else
                    {
                        vm.ListOption.Add(new OptionModel { OptionName = "" });
                        vm.ListOption.Add(new OptionModel { OptionName = "" });
                    }
                    break;
                case (int)(EnumQtType.TrueFalse):
                    vm.ListOption.Add(new OptionModel { OptionName = "是" });
                    vm.ListOption.Add(new OptionModel { OptionName = "否" });

                    if (vm.Data.qtType == (int)(EnumQtType.TrueFalse) && vm.Data.listItemInfo != null)
                    {
                        var optionItem1 = vm.Data.listItemInfo.FirstOrDefault(x => x.orderId == 0);
                        var optionItem2 = vm.Data.listItemInfo.FirstOrDefault(x => x.orderId == 1);

                        if (optionItem1 != null)
                        {
                            vm.ListOption[0].Score = optionItem1.score;
                        }
                        if (optionItem2 != null)
                        {
                            vm.ListOption[1].Score = optionItem2.score;
                        }
                    }
                    break;
            }
        }

        private async void BtnClick_Create(object sender, RoutedEventArgs e)
        {
            CreateQuestionBankDto createDto = new CreateQuestionBankDto()
            {
                qbId = vm.Data.qbId,
                title = vm.Title,
                examType = (int)KPIData.ExamType,
                qtType = vm.SelectedQtType.Key,
                creatorCode = vm.Data.creatorCode,
                createTime = vm.Data.createTime,
            };
            if (createDto.qtType != (int)EnumQtType.ShortAnswer)
            {
                foreach (var item in vm.ListOption)
                {
                    createDto.listItemInfo.Add(new QuestionItemInfoVO
                    {
                        content = item.OptionName,
                        score = item.Score,
                        orderId = createDto.listItemInfo.Count,
                    });
                }
                createDto.score = createDto.listItemInfo.Sum(x => x.score);
            }
            else
            {
                createDto.score = vm.Score;
            }

            var rData = await Run(() => QuestionBankApi.UpdateQuestionBank(createDto));

            if (rData == null)
            {
                MessageBox.Show("数据异常,请检查日志!");
                return;
            }
            if (rData.code != 0)
            {
                MessageBox.Show("数据异常,code:" + rData.code + ":msg:" + rData.msg);
                return;
            }
            this.DialogResult = true;
        }

        private void BtnClick_Cancle(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }



        private void BtnClick_AddOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);

            if (index >= 0)
            {
                vm.ListOption.Insert(index + 1, new OptionModel { });
            }
        }

        private void BtnClick_DeleteOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index >= 0)
            {
                vm.ListOption.RemoveAt(index);
            }
        }

        private void BtnClick_UpOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index > 0)
            {
                vm.ListOption.RemoveAt(index);
                vm.ListOption.Insert(index - 1, dx);
            }
        }

        private void BtnClick_AddScore(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            dx.Score++;
        }

        private void BtnClick_ReduceScore(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            dx.Score--;
            if (dx.Score < 0)
                dx.Score = 0;
        }

        private void BtnClick_DownOption(object sender, RoutedEventArgs e)
        {
            var dx = (sender as Button).DataContext as OptionModel;
            var index = vm.ListOption.IndexOf(dx);
            if (index + 1 < vm.ListOption.Count)
            {
                vm.ListOption.RemoveAt(index);
                vm.ListOption.Insert(index + 1, dx);
            }
        }
        private void BtnClick_AddScore_ShortAnswer(object sender, RoutedEventArgs e)
        {
            vm.Score++;
        }

        private void BtnClick_ReduceScore_ShortAnswer(object sender, RoutedEventArgs e)
        {
            vm.Score--;
            if (vm.Score < 0)
                vm.Score = 0;
        }
    }
}
